package bussinessService.impl;

import java.sql.SQLException;
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import module.Employees;
import module.Shop;
import module.WorkSchedule;
import utils.DateUtil;
import bussinessService.workscheduleBusinessService;
import domain.vo.EmployeesQueryObject;
import domain.vo.WorkScheduleQueryObject;
import domain.vo.WorkScheduleVO;
import domain.vo.queryObject;

public class WorkScheduleBusinessServiceImpl implements
		workscheduleBusinessService {

	/*
	 * (非 Javadoc) <p>Title: getById</p> <p>Description: </p>
	 * 
	 * @param id
	 * 
	 * @return
	 * 
	 * @throws SQLException
	 * 
	 * @see bussinessService.basicbussinessService#getById(java.lang.String)
	 */
	@Override
	public WorkSchedule getById(String id) throws SQLException {
		return workScheduleService.getById(id);
	}

	/*
	 * (非 Javadoc) <p>Title: getAll</p> <p>Description: </p>
	 * 
	 * @return
	 * 
	 * @throws SQLException
	 * 
	 * @see bussinessService.basicbussinessService#getAll()
	 */
	@Override
	public List<WorkScheduleVO> getAll() throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}

	/*
	 * (非 Javadoc) <p>Title: merageObject</p> <p>Description: </p>
	 * 
	 * @param t
	 * 
	 * @throws SQLException
	 * 
	 * @see
	 * bussinessService.basicbussinessService#merageObject(java.lang.Object)
	 */
	@Override
	public void merageObject(WorkSchedule t) throws SQLException {
		Shop shop = shopService.getById(Integer.toString(t.getShopId()));
		t.setShopName(shop.getShopName());
		workScheduleService.insertObject(t);

	}

	/*
	 * (非 Javadoc) <p>Title: deleteT</p> <p>Description: </p>
	 * 
	 * @param id
	 * 
	 * @throws SQLException
	 * 
	 * @see bussinessService.basicbussinessService#deleteT(int)
	 */
	@Override
	public void deleteT(int id) throws SQLException {
		// TODO Auto-generated method stub

	}

	/*
	 * (非 Javadoc) <p>Title: getObjectByQueryObject</p> <p>Description: </p>
	 * 
	 * @param queryObject
	 * 
	 * @return
	 * 
	 * @throws SQLException
	 * 
	 * @see
	 * bussinessService.basicbussinessService#getObjectByQueryObject(domain.
	 * vo.queryObject)
	 */
	@Override
	public List<WorkScheduleVO> getObjectByQueryObject(queryObject queryObject)
			throws SQLException {
		List<WorkScheduleVO> vos = new ArrayList<WorkScheduleVO>();
		List<WorkSchedule> results = new ArrayList<WorkSchedule>();
		results = workScheduleService.getObjectByQueryObject(queryObject);
		Map<String, List<Double>> data = new HashMap<String, List<Double>>();
		List<Double> times = null;
		// fill data from db
		fillData(results, data, times);
		// initial vo
		WorkScheduleVO vo = new WorkScheduleVO();
		initVO(vo,vos,queryObject);
		// 组装需要显得VO
		packDataVO(vo, vos, data, times);
		return vos;
	}

	/**
	 * 
	 * @Title: packDataVO
	 * @Description:组装需要显得VO
	 * @param @param vo vo
	 * @param @param vos vo集合
	 * @param @param data 从数据库放入的数据集合
	 * @param @param times 从数据库放入的时间数据集合
	 * @return void 返回类型
	 * @throws
	 */
	private void packDataVO(WorkScheduleVO vo, List<WorkScheduleVO> vos,
			Map<String, List<Double>> data, List<Double> times) {
		int index = 0;
		vo = vos.get(index);
		for (String key : vo.getSpecificLables()) {
			if (data.containsKey(key)) {
				times = data.get(key);
				vo.getStart().remove(index);
				vo.getStart().add(index, times.get(0));
				if (times.size() > 1) {
					vo.getEnd().remove(index);
					vo.getEnd().add(index, times.get(1));
				}
			}
			index++;
		}
	}

	/**
	 * 
	 * @Title: fillData
	 * @Description: 填充数据
	 * @param @param results DB数据集合
	 * @param @param data 需要填充的数据集合
	 * @param @param times 时间集合
	 * @return void 返回类型
	 * @throws
	 */
	private void fillData(List<WorkSchedule> results,
			Map<String, List<Double>> data, List<Double> times) {
		for (WorkSchedule ws : results) {
			String day = DateUtil.date2String(ws.getDay(), DateUtil.YYYY_MM_DD);
			String key = day + "/" + ws.getE_name();
			if (!data.containsKey(key)) {
				times = new ArrayList<Double>();
				times.add(Double.parseDouble(DateUtil.date2String(ws.getTime(),
						DateUtil.HH_mm)));
				data.put(key, times);
			} else {
				times = data.get(key);
				times.add(Double.parseDouble(DateUtil.date2String(ws.getTime(),
						DateUtil.HH_mm)));
			}

		}

	}

	/**
	 * 
	 * @Title: initVO
	 * @Description: 初始化VO
	 * @param @param vo 需要初始化的VO
	 * @param @throws SQLException 设定文件
	 */
	private void initVO(WorkScheduleVO vo, List<WorkScheduleVO> vos, queryObject queryObject)
			throws SQLException {
		EmployeesQueryObject eqo = new EmployeesQueryObject();
		eqo.setShopId(Integer.parseInt(((WorkScheduleQueryObject)queryObject).getShopId()));
		// 获取所有员工
		List<Employees> employees = employeesService.getObjectByQueryObject(eqo);
		int i = 0;

		Calendar calendar = ((WorkScheduleQueryObject)queryObject).getCalendar();
		calendar.set(Calendar.DAY_OF_WEEK, calendar.getFirstDayOfWeek());
		StringBuffer sb = new StringBuffer();
		for (i = 1; i <= 7;) {
			for (Employees emp : employees) {
				String day = DateUtil.date2String(calendar.getTime(),
						DateUtil.YYYY_MM_DD);
				sb.append(day);
				sb.append("/");
				sb.append(emp.getE_name());
				vo.getSpecificLables().add(sb.toString());
				vo.getStart().add(0d);
				vo.getEnd().add(0d);
				vo.getLable().add(25d);
				sb.delete(0, sb.length());
			}
			calendar.set(Calendar.DAY_OF_WEEK, ++i);
		}
		vos.add(vo);
	}

	/*
	 * (非 Javadoc) <p>Title: countByQueryObject</p> <p>Description: </p>
	 * 
	 * @param queryObject
	 * 
	 * @return
	 * 
	 * @throws SQLException
	 * 
	 * @see bussinessService.basicbussinessService#countByQueryObject(domain.vo.
	 * queryObject)
	 */
	@Override
	public int countByQueryObject(queryObject queryObject) throws SQLException {
		return workScheduleService.countByQueryObject(queryObject);
	}

}
